import { setContextMenu } from '@/views/components/contextmenu/data';
import {
  CompassTwoTone,
  DeleteOutlined,
  FileImageOutlined,
  FileOutlined,
  FolderOpenOutlined,
  FolderOutlined,
  GoogleOutlined,
  ReloadOutlined,
  RetweetOutlined,
} from '@ant-design/icons-vue';
import type { DataNode } from 'ant-design-vue/es/tree';

import { viewFile } from '@/api/modules/tool/file/file';
import { openBrowser, openImage } from '@/global/window/widget';
import { blobToText, toBase64 } from '@/utils/file/file';
import { delFile, loadPath, openFile } from './action';
import { selectedFile, selectedFolders } from './data';
import { createFile, createFolder, editFileName, openWithNotepad } from './file.curd';

export const parentMenus = [
  {
    label: '创建文件夹',
    icon: h(FolderOutlined),
    key: 'folder',
    async onClick() {
      createFolder();
    },
  },
  {
    label: '创建文件',
    icon: h(FileOutlined),
    key: 'file',
    onClick() {
      createFile();
    },
  },
  {
    label: '刷新',
    icon: h(ReloadOutlined),
    key: 'reload',
    onClick() {
      loadPath();
    },
  },
];
export const rootContextmenu = (e: MouseEvent) => {
  setContextMenu({
    show: true,
    items: parentMenus,
  });
};
const getFileData = async () => {
  if (selectedFile.value) {
    return await viewFile(`${selectedFile.value.key}`);
  }
};

export const fileContextmenu = (item: DataNode) => {
  selectedFolders.value = [item.key];
  selectedFile.value = item;
  console.log(item);
  let disabled = item.type === 'folder';
  setContextMenu({
    show: true,
    items: [
      {
        label: '打开',
        icon: h(FolderOpenOutlined),
        key: 'folder',
        onClick() {
          openFile(item);
        },
      },
      {
        label: '删除',
        icon: h(DeleteOutlined),
        key: 'delete',
        onClick() {
          delFile();
        },
      },
      {
        label: '重命名',
        icon: h(RetweetOutlined),
        key: 'rename',
        onClick() {
          editFileName();
        },
      },
      {
        label: '打开方式',
        icon: h(CompassTwoTone),
        key: 'rename',
        disabled,
        children: [
          {
            label: '记事本',
            icon: h(FileOutlined),
            key: 'notepad',
            async onClick() {
              const resp = await getFileData();
              const text = await blobToText(resp);
              openWithNotepad(text);
            },
          },
          {
            label: '图片',
            icon: h(FileImageOutlined),
            key: 'notepad',
            async onClick() {
              const resp = await getFileData();
              const base64 = await toBase64(resp);
              console.log(base64);

              openImage({
                src: base64,
                mode: 'view',
              });
            },
          },
          {
            label: '浏览器',
            icon: h(GoogleOutlined),
            key: 'notepad',
            async onClick() {
              const resp = await getFileData();
              const text = await blobToText(resp);
              openBrowser({
                src: text,
                type: 'html',
              });
            },
          },
        ],
      },
    ],
  });
};
